package virtualstock.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;

import javax.ejb.EJBException;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "portfolio", schema = "vstock")
@NamedQuery(name="Portfolio.FindByGameAndPlayer", query = "SELECT a.portfolioID FROM Portfolio a WHERE a.gameID = :gameID AND a.playerID = :playerID")
public class Portfolio implements Serializable {
	@Id
	@Column(name = "portfolioID")
	private Long portfolioID;
	
	@Column(name = "gameID")
	private Long gameID;

	@Column(name = "playerID")
	private Long playerID;

	@Column(name = "balance")
	private BigDecimal balance;
	
	@JoinTable(name = "stock_in_portfolio", schema = "vstock", joinColumns = @JoinColumn(name = "portfolioID", referencedColumnName = "portfolioID")
		    , inverseJoinColumns = @JoinColumn(name = "stockID", referencedColumnName = "stockID")
		    )
	@ManyToMany
	private Collection<Stock> stocks;

	private static final long serialVersionUID = 1L;
	
	public Portfolio() {
		super();
	}

	public Portfolio(
			Long gameID, 
			Long playerID,
			BigDecimal balance) {
		this.gameID = gameID;
		this.playerID = playerID;
		this.balance = balance;
	}

	public Long getPortfolioID() {
		return this.portfolioID;
	}

	public void setPortfolioID(Long portfolioID) {
		this.portfolioID = portfolioID;
	}

	public Long getGameID() {
		return this.gameID;
	}

	public void setGameID(Long gameID) {
		this.gameID = gameID;
	}

	public Long getPlayerID() {
		return this.playerID;
	}

	public void setPlayerID(Long playerID) {
		this.playerID = playerID;
	}

	public BigDecimal getBalance() {
		return this.balance;
	}

	public void setBalance(BigDecimal balance) {
		this.balance = balance;
	}
	
	public void setStocks(Collection<Stock> stocks) {
		this.stocks = stocks;
	}

	public Collection<Stock> getStocks() {
		return stocks;
	}
	
	public void addStock(Stock stock) {
		try {
			this.stocks.add(stock);
		} catch (Exception ex) {
			throw new EJBException(ex);
		}
	}
	
	public void removeStock(Stock stock) {
		try {
			this.stocks.remove(stock);
		} catch (Exception ex) {
			throw new EJBException(ex);
		}
	}

}
